#!/bin/bash

# YanXue AI Go 项目启动脚本

set -e

echo "🚀 启动 YanXue AI Go 微服务平台..."

# 检查环境变量文件
if [ ! -f .env ]; then
    echo "📋 .env 文件不存在，从模板创建..."
    cp .env.example .env
    echo "✅ 已创建 .env 文件，请根据需要修改配置"
fi

# 检查Go版本
echo "🔍 检查Go版本..."
go version

# 检查依赖
echo "📦 检查Go模块依赖..."
go mod tidy

# 创建必要的目录
echo "📁 创建必要的目录..."
mkdir -p uploads
mkdir -p logs

# 根据参数选择启动模式
case "$1" in
    "gateway")
        echo "🌐 启动API网关服务..."
        cd services/gateway/cmd && go run main.go
        ;;
    "agent")
        echo "🤖 启动Agent服务..."
        cd services/agent/cmd && go run main.go
        ;;
    "mcp")
        echo "🔗 启动MCP服务..."
        cd services/mcp/cmd && go run main.go
        ;;
    "rag")
        echo "📚 启动RAG服务..."
        cd services/rag/cmd && go run main.go
        ;;
    "all")
        echo "🚀 启动所有服务..."
        echo "请使用 Docker Compose 或分别在不同终端启动各个服务"
        echo "使用方法："
        echo "  ./start.sh gateway  # 启动网关服务"
        echo "  ./start.sh agent    # 启动Agent服务"
        echo "  ./start.sh mcp      # 启动MCP服务"
        echo "  ./start.sh rag      # 启动RAG服务"
        ;;
    "build")
        echo "🔨 构建所有服务..."
        make build
        ;;
    "test")
        echo "🧪 运行测试..."
        go test -v ./...
        ;;
    "dev-setup")
        echo "⚙️ 设置开发环境..."
        make dev-setup
        ;;
    *)
        echo "使用方法: $0 {gateway|agent|mcp|rag|all|build|test|dev-setup}"
        echo ""
        echo "可用命令:"
        echo "  gateway   - 启动API网关服务 (端口: 8080)"
        echo "  agent     - 启动Agent服务 (端口: 8081)"
        echo "  mcp       - 启动MCP服务 (端口: 8082)"
        echo "  rag       - 启动RAG服务 (端口: 8083)"
        echo "  all       - 显示如何启动所有服务"
        echo "  build     - 构建所有服务"
        echo "  test      - 运行测试"
        echo "  dev-setup - 设置开发环境"
        echo ""
        echo "示例:"
        echo "  $0 gateway    # 启动网关服务"
        echo "  $0 build      # 构建项目"
        echo "  $0 dev-setup  # 设置开发环境"
        exit 1
        ;;
esac

echo "✅ 完成!"